package com.mall.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;


	//	springMVC 全局异常 . 类写完后,还需在spring中注册该bean.

public class ExceptionResolver implements HandlerExceptionResolver{
	
	Logger logger = LoggerFactory.getLogger(ExceptionResolver.class);
	 
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
										Object handler,Exception ex) {
		
		//打印日志
		logger.error("{} exception",request.getRequestURI(),ex);
		 
		ModelAndView view = new ModelAndView(new MappingJacksonJsonView());
			
		//	当使用是jackson2.x的时候使用MappingJackson2JsonView,
		//  在这里使用的是1.9
		
		view.addObject("status",ResponseCode.ERROR.getCode());
		view.addObject("msg","接口异常,详情请查看服务端日志信息");
		view.addObject("data",ex.toString());
		
		return view;
	}
	
}
